import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from 'vue-router'
// import { useStore } from '@/store/index'
// import { storeToRefs } from 'pinia'
import Cookies from 'js-cookie'

const routes: RouteRecordRaw[] = [
  // 测试单个组件用
  {
    path: '/test',
    name: '调试单个组件用',
    // component: () => import('@/components/foreground/affix/Menu.vue'), // 移动端munu
    // component: () => import('@/components/backstage/header/appBar.vue'), //  
    component: () => import('@/components/backstage/main/articleOperation/addArticle/previewArticle/index.vue'), // 修改i文章  
    // component: () => import('@/components/foreground/main/classify/content/mainContent/index.vue'), //  文章详情
    // component: () => import('@/components/foreground/main/articleDetails/content/commentsSection/index.vue'), // 评论
    // component: () => import('@/components/foreground/footer/Copyright.vue'), // Copyright
    // component: () => import('@/components/foreground/header/Menu.vue'), // topMenu
    // component: () => import('@/components/foreground/main.vue'), // main
    // component: () => import('@/components/foreground/main/home/home.vue'), //home
    // component: () => import('@/components/foreground/otherModules/websiteInfo/index.vue'), //站点信息
    // component: () => import('@/components/foreground/otherModules/articleCategories/index.vue'), //文章分类
    // component: () => import('@/components/foreground/otherModules/resource/index.vue'), //资源
    // component: () => import('@/components/foreground/otherModules/featured/index.vue'), //精选
    // component: () => import('@/components/foreground/otherModules/technology/index.vue'), // 技术

    // component: () => import('@/components/foreground/main/home/content/center/recentPosts.vue'), // 置顶
    // component: () => import('@/components/foreground/main/home/content/banner/Carousel.vue'),  //轮播
  },
  {
    // 文章详情
    path: "/articleDetails/:id",
    name: 'articleDetails',
    component: () => import('@/components/foreground/main/articleDetails/articleDetails.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/components/login/index.vue'),
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/components/register/index.vue'),
  },
  {
    path: '/resetPassword',
    name: 'resetPassword',
    component: () => import('@/components/resetPassword/index.vue'),
  },
  // 前台路由
  {
    path: "/foreground",
    name: "首页",
    meta: { title: 'ToLzb - 每天都是不一样' },
    component: () => import('@/components/foreground/main.vue'),
    redirect: "/",
    children: [{
      path: "/",
      name: "首页",
      meta: { title: 'ToLzb - 每天都是不一样' },
      component: () => import('@/components/foreground/main/home/home.vue'),
    }, {
      // 搜索
      path: "/search",
      component: () => import('@/components/foreground/main/search/search.vue')
    }, {
      // 文章分类
      path: "/classify",
      component: () => import('@/components/foreground/main/classify/classify.vue')
    }, {
      // 文章详情
      path: "/articleDetails",
      name: 'articleDetails',
      component: () => import('@/components/foreground/main/articleDetails/articleDetails.vue')
    }, {
      path: "/article/",
      name: "全部文章",
      meta: { title: 'ToLzb - 全部文章' },
      component: () => import('@/components/foreground/main/article/article.vue')

    }, {
      path: "/website",
      name: "网站链接",
      meta: { title: 'ToLzb - 网站链接' },
      component: () => import('@/components/foreground/main/website/website.vue')
    }, {
      path: "/message",
      name: "留言",
      meta: { title: 'ToLzb - 留言' },
      component: () => import('@/components/foreground/main/message/message.vue')
    }, {
      path: "/about",
      name: "关于",
      meta: { title: 'ToLzb - 关于' },
      component: () => import('@/components/foreground/main/about/about.vue')
    },
    {
      path: "/message",
      name: "留言",
      meta: { title: 'ToLzb - 留言' },
      component: () => import('@/components/foreground/main/message/message.vue')
    }, {
      // 404
      path: '/:pathMatch(.*)*',
      name: "404",
      meta: { title: 'ToLzb - 404' },
      component: () => import('../components/state/404/404.vue')
    }, {
      // 403
      path: '/403',
      name: "403",
      meta: { title: 'ToLzb - 403' },
      component: () => import('../components/state/403/403.vue')
    },
    ]
  }, {
    path: "/backstage",
    name: "backstage",
    meta: { title: 'ToLzb - 后台首页', chineseName: '首页', icon: 'House' },
    component: () => import('../components/backstage/main.vue'),
    redirect: "/backstageHome",
    children: [{
      path: "/backstageHome",
      name: "backstageHome",
      meta: { title: 'ToLzb - 后台首页', chineseName: '后台首页', icon: 'House' },
      component: () => import('../components/backstage/main/backstageHome/backstageHome.vue')
    }, {
      path: "/addArticle",
      name: "addArticle",
      meta: { title: 'ToLzb - 新建文章', chineseName: '新建文章', icon: 'EditPen' },
      component: () => import('../components/backstage/main/articleOperation/addArticle/addArticle.vue')
    }, {
      path: "/articleManager",
      name: "articleManager",
      meta: { title: 'ToLzb - 文章管理', chineseName: '文章管理', icon: 'Edit' },
      component: () => import('../components/backstage/main/articleOperation/articleManager/articleManager.vue')
    }, {
      path: "/classifyManager",
      name: "classifyManager",
      meta: { title: 'ToLzb - 分类管理', chineseName: '分类管理', icon: 'Discount' },
      component: () => import('../components/backstage/main/articleOperation/classifyManager/classifyManager.vue')
    }, {
      path: "/labelManager",
      name: "labelManager",
      meta: { title: 'ToLzb - 标签管理', chineseName: '标签管理' },
      component: () => import('../components/backstage/main/articleOperation/labelManager/labelManager.vue')
    }, {
      // 评论管理
      path: '/commentManager',
      name: '评论管理',
      meta: { title: 'ToLzb - 评论管理', chineseName: '评论管理' },
      component: () => import("../components/backstage/main/commentManager/commentManager.vue"),
    }, {
      path: "/userManager",
      name: "userManager",
      meta: { title: 'ToLzb - 用户管理', chineseName: '用户管理' },
      component: () => import('../components/backstage/main/userManager/userManager.vue')
    }, {
      // 链接管理
      path: '/linkManager',
      name: '链接管理',
      meta: { title: 'ToLzb - 链接管理', chineseName: '链接管理' },
      component: () => import("../components/backstage/main/linkManager/linkManager.vue"),
    }, {
      // 个人设置
      path: '/personSetup',
      name: '个人设置',
      meta: { title: 'ToLzb - 个人设置', chineseName: '个人设置' },
      component: () => import("../components/backstage/main/personSetup/personSetup.vue"),
    }, {
      // 系统设置
      path: '/systemSetup',
      name: '系统设置',
      meta: { title: 'ToLzb - 系统设置', chineseName: '系统设置' },
      component: () => import("../components/backstage/main/systemSetup/systemSetup.vue"),
    }, {
      // 轮播图
      path: '/carouseMap',
      name: '轮播图',
      meta: { title: 'ToLzb - 轮播图', chineseName: '轮播图' },
      component: () => import("../components/backstage/main/carouseMap/carouseMap.vue"),
    },
    {
      // 循环字
      path: '/circulateText',
      name: '循环字',
      meta: { title: 'ToLzb - 循环字', chineseName: '循环字' },
      component: () => import("../components/backstage/main/circulateText/circulateText.vue"),
    }, {
      // 登录信息
      path: '/visitIp',
      name: '访问信息',
      meta: { title: 'ToLzb - 访问信息', chineseName: '访问信息' },
      component: () => import("../components/backstage/main/visitIp/visitIp.vue"),
    }]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior() {
    return { top: 0 }
  },
})
// 白名单
const whiteList = ['/systemSetup', '/linkManager', "/userManager", "/classifyManager", "/labelManager", '/personSetup', "/backstage", '/commentManager', "/articleManager", "/addArticle", "/backstageHome",];
const adminList = ['/systemSetup', '/linkManager', "/userManager", "/classifyManager", "/labelManager",];
router.beforeEach(async (to, from, next) => {
  // const Store = useStore()
  // const { LoginInfo } = storeToRefs(Store)
  // const { code: LoginCode, data: LoginData } = LoginInfo.value;
  const LoginState = Cookies.get('LoginState')
  const UserType = Cookies.get('UserType')
  // 验证是否登录
  // 登录还访问登录界面 就跳转到网站首页
  if (LoginState === 'yes' && to.path === "/login") {
    return next("/")
  } else {
    // 如果to.path 不包含在白名单内，放行
    if (!whiteList.includes(to.path)) {
      next(); // 放行
    } else {
      if (LoginState !== 'yes') {
        return next("/login")
      } else {
        if (adminList.includes(to.path) && UserType === '1') {
          // 后台部分界面普通用户不允许访问，比如：用户管理
          return next("/403")
        } else {
          next();
        }
      }
    }
  }
})

router.afterEach((to, from, failure) => {

})

export { router }
